﻿using System;
using NLog;
using System.Configuration;
using CYP.IISMonitor.Utility.Mails;

namespace CYP.IISMonitor.Utility.Log
{
    /// <summary>
    /// 封装NLog记录文本日志的方法
    /// <para>提供了记录日志产生的位置，异常信息及格式化输出的能力</para>
    /// </summary>
    public class TextLogger
    {
        /// <summary>
        /// 日志记录实例
        /// </summary>
        private static readonly Logger _log = LogManager.GetCurrentClassLogger();

        /// <summary>
        /// 日志
        /// </summary>
        private readonly string _logger;

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="logger">日志内容</param>
        public TextLogger(string logger)
        {
            _logger = logger;
        }

        /// <summary>
        ///     记录正常信息日志
        /// </summary>
        /// <param name="format">格式化</param>
        /// <param name="args">参数</param>
        public void Info(string format, params object[] args)
        {
            WriteLog(LogLevel.Info, null, format, args);
        }

        /// <summary>
        ///     记录错误信息日志
        /// </summary>
        /// <param name="format">格式化</param>
        /// <param name="args">参数</param>
        public void Error(string format, params object[] args)
        {
            WriteLog(LogLevel.Error, null, format, args);
        }

        /// <summary>
        ///     记录错误信息日志
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="format">格式化</param>
        /// <param name="args">参数</param>
        public void Error(Exception ex, string format, params object[] args)
        {
            WriteLog(LogLevel.Error, ex, format, args);
        }

        /// <summary>
        ///     记录警告信息日志
        /// </summary>
        /// <param name="format">格式化</param>
        /// <param name="args">参数</param>
        public void Warn(string format, params object[] args)
        {
            WriteLog(LogLevel.Warn, null, format, args);
        }

        /// <summary>
        ///     记录警告信息日志
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="format">格式化</param>
        /// <param name="args">参数</param>
        public void Warn(Exception ex, string format, params object[] args)
        {
            WriteLog(LogLevel.Warn, ex, format, args);
        }

        /// <summary>
        ///     记录跟踪日志
        /// </summary>
        /// <param name="format">格式化</param>
        /// <param name="args">参数</param>
        public void Trace(string format, params object[] args)
        {
            WriteLog(LogLevel.Trace, null, format, args);
        }

        /// <summary>
        /// 记录跟踪日志
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="format">格式化</param>
        /// <param name="args">参数</param>
        public void Trace(Exception ex, string format, params object[] args)
        {
            WriteLog(LogLevel.Trace, ex, format, args);
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="level">等级</param>
        /// <param name="ex">异常</param>
        /// <param name="format">格式化格式</param>
        /// <param name="args">参数</param>
        private void WriteLog(LogLevel level, Exception ex, string format, params object[] args)
        {
            string message = string.Format(format ?? string.Empty, args);
            if (ex != null)
            {
                message = string.Concat(message, "\r\n", ex.Message);
            }

            message = string.Concat(_logger, "\r\n", message);
            message = string.Concat(message, "\r\n--------------------------------------------------------\r\n");
            _log.Log(level, message);
        }

        /// <summary>
        /// 功    能: 发送系统异常邮件
        /// </summary>
        /// <param name="errorMsg">异常消息</param>
        private void SendMailH(string errorMsg)
        {
            try
            {
                string strMail = ConfigurationManager.AppSettings["eMail"];
                if (string.IsNullOrEmpty(strMail))
                {
                    return;
                }

                SendMail sendMail = SendMail.GetInstance("dispute");
                sendMail.DisplayName = "DisplayName";
                sendMail.SendAsyncHtml("Subject", strMail, "MailBody");
            }
            catch (Exception ex)
            {
                this.Error(ex.Message);
            }
        }
    }
}
